I’ve trying to use MySQL Connector C++ for my project, here the example code (retrieved from official site):
mysqlconn.cpp:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
#include <iostream> #include "mysql_connection.h" #include <cppconn/driver.h> #include <cppconn/exception.h> #include <cppconn/resultset.h> #include <cppconn/statement.h> using namespace std; #define EXAMPLE_HOST "localhost" #define EXAMPLE_USER "root" #define EXAMPLE_PASS "get your own password here" #define EXAMPLE_DB "information_schema" int main(int argc, const char **argv) { const string url(argc >= 2 ? argv[1] : EXAMPLE_HOST); const string user(argc >= 3 ? argv[2] : EXAMPLE_USER); const string pass(argc >= 4 ? argv[3] : EXAMPLE_PASS); const string database(argc >= 5 ? argv[4] : EXAMPLE_DB); try { sql::Driver *driver; sql::Connection *con; sql::Statement *stmt; sql::ResultSet *res; driver = get_driver_instance(); /* Create a connection */ con = driver->connect("tcp://" + url + ":3306", user, pass); con->setSchema(database); /* Set the database */ stmt = con->createStatement(); res = stmt->executeQuery("SELECT 'Hello World!' AS _message"); while (res->next()) { cout << "\t... MySQL replies: "; cout << res->getString("_message") << endl; cout << "\t... MySQL says it again: "; cout << res->getString(1) << endl; } delete res; delete stmt; delete con; } catch (sql::SQLException &e) { /* The MySQL Connector/C++ throws three different exceptions: - sql::MethodNotImplementedException (derived from sql::SQLException) - sql::InvalidArgumentException (derived from sql::SQLException) - sql::SQLException (derived from std::runtime_error) */ cout << "# ERR: SQLException in " << __FILE__; cout << "(" << __FUNCTION__ << ") on line " << __LINE__ << endl; /* Use what() (derived from std::runtime_error) to fetch the error message */ cout << "# ERR: " << e.what(); cout << " (MySQL error code: " << e.getErrorCode(); cout << ", SQLState: " << e.getSQLState() << " )" << endl; return EXIT_FAILURE; } return EXIT_SUCCESS; } |
To identify which argument for linker is quite easy:
1 2 3 4 5 6 |
% ldconfig -p |grep mysql libmysqlcppconn.so.4 (libc6,x86-64) => /usr/lib/libmysqlcppconn.so.4 libmysqlcppconn.so (libc6,x86-64) => /usr/lib/libmysqlcppconn.so libmysqlclient_r.so.16 (libc6,x86-64) => /usr/lib/libmysqlclient_r.so.16 libmysqlclient.so.16 (libc6,x86-64) => /usr/lib/libmysqlclient.so.16 |
However, when I try to compile it, error happened (missing boost library)
1 2 3 4 5 6 7 8 9 |
% g++ mysqlconn.cpp -lmysqlcppconn In file included from /usr/include/mysql_connection.h:15, from mysqlconn.cpp:3: /usr/include/cppconn/connection.h:16:29: error: boost/variant.hpp: No such file or directory In file included from mysqlconn.cpp:3: /usr/include/mysql_connection.h:16:32: error: boost/shared_ptr.hpp: No such file or directory In file included from /usr/include/mysql_connection.h:15, ... |
Quick fix:
1 |
% sudo apt-get install libboost-dev |
Now run the sample code:
% g++ mysqlconn.cpp -lmysqlcppconn; ./a.out
... MySQL replies: Hello World!
... MySQL says it again: Hello World!